package com.elibrary;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import nimo.elibrary.json.model.book.Book;
import nimo.elibrary.json.model.book.InPress;

/**
 * Created with IntelliJ IDEA.
 * User: Enkuso
 * Date: 1/28/13
 * Time: 10:14 AM
 * To change this template use File | Settings | File Templates.
 */
public class OfflineBookDetailFragment extends CountingFragment {
    public TextView bookTitle;
    public TextView bookAuthor;
    public TextView bookIntroduction;
    public ImageView bookCover;
    public TextView bookTags;
    public TextView bookCategoryAndPublishedYear;
    public TextView bookPageCount;
    public TextView bookRating;
    public LinearLayout bookReviews;
    public LinearLayout bookInPressContainer;
    public LinearLayout relatedBooks;
    public Button btnMoreReviews;
    Book book;

    Context context;
    View v;
    LayoutInflater inflater;

    public OfflineBookDetailFragment() {

    }

    public OfflineBookDetailFragment(int num, Book book) {
        super(num);
        context = BookstoreApplication.getAppContext();
        this.book = book;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.book_detail, container, false);
        this.inflater = inflater;

        // get text views
        bookTitle = (TextView) v.findViewById(R.id.tvBookTitle);
        bookAuthor = (TextView) v.findViewById(R.id.tvBookAuthor);
        bookIntroduction = (TextView) v.findViewById(R.id.tvBookInformation);
        bookCover = (ImageView) v.findViewById(R.id.imgBookCover);
        bookTags = (TextView) v.findViewById(R.id.tvBookCategoryAndPublishedYear);
        bookCategoryAndPublishedYear = (TextView) v.findViewById(R.id.tvBookCategoryAndPublishedYear);
        bookPageCount = (TextView) v.findViewById(R.id.tvBookPageCount);
        bookRating = (TextView) v.findViewById(R.id.tvBookRating);
        bookReviews = (LinearLayout) v.findViewById(R.id.reviews);
        bookInPressContainer = (LinearLayout) v.findViewById(R.id.inPressContainer);
        relatedBooks = (LinearLayout) v.findViewById(R.id.relatedBooksContainer);

        btnMoreReviews = (Button) v.findViewById(R.id.btnLoadMoreReviews);

        setBook();

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        ((BaseActivity)getSherlockActivity()).currentFragment = Constants.FRAGMENT_BOOKDETAIL;
    }

    private void setBook(){

        bookTitle.setText(book.title);
        bookAuthor.setText(book.authorName);
        bookIntroduction.setText(book.introduction);
        bookPageCount.setText(String.valueOf(book.pageCount) + " " + getString(R.string.page));

        // cover zurag
        ((BookstoreApplication)context.getApplicationContext()).imageLoader.DisplayImage(book.cover, bookCover);

        // tags
        String tag = "";
        if(book.tags != null) {
            int tag_len = book.tags.length - 1;
            for (int i=0; i<=tag_len; i++){
                tag += book.tags[i];
                if(i != tag_len)
                    tag += ", ";
            }
            bookTags.setText(tag);
        }

        // rating
        bookRating.setText(book.rating + " (" + book.ratesCount + " " + getString(R.string.rates) + ")");
        int rate = (int)book.rating;
        int[] star = {R.drawable.star0, R.drawable.star1, R.drawable.star2, R.drawable.star3, R.drawable.star4, R.drawable.star5};
        bookRating.setCompoundDrawablesWithIntrinsicBounds(star[rate], 0, 0, 0);

        btnMoreReviews.setVisibility(View.GONE);
        ((LinearLayout)bookReviews.getParent()).setVisibility(View.GONE);

        ((LinearLayout)relatedBooks.getParent()).removeView(relatedBooks);

        // Бүтээлийн талаар хэвлэл мэдээллээр
        if(book.inPress != null && book.inPress.size() > 0 ) {
            bookInPressContainer.removeAllViews();
            for(InPress inPress : book.inPress) {
                ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.hewlel_medeelel, null);
                TextView title = (TextView) viewGroup.findViewById(R.id.tvInPressTitle);
                TextView body = (TextView) viewGroup.findViewById(R.id.tvInPressBody);
                TextView source = (TextView) viewGroup.findViewById(R.id.tvInPressSource);
                title.setText(inPress.title);
                body.setText(inPress.body);
                source.setText(inPress.source);
                bookInPressContainer.addView(viewGroup);
            }
        }
        else {
            ((LinearLayout)bookInPressContainer.getParent()).removeView(bookInPressContainer);
        }

        // set currently viewing book
        ((BaseActivity)getSherlockActivity()).setCurrentlyViewingBook(book);
    }
}
